var app = getApp()
Page({
    data: {
        code: 0
    },

    onShow() {
        if (app.data.refresh) {
            that.getdata()
            app.data.refresh = 0
        }
    },

    onLoad() {
        var that = this
        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res
                },() => {
                    that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo
            }, () => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const { uid,openid } = that.data.userinfo
        const data = { uid,openid }
        // console.log(data)
        app.api_request('?c=user','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res,
                },() => {
                    that.getlist()
                })
                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },

    getlist() {
        var that = this
        const { uid,openid } = that.data.userinfo
        const data = { uid,openid }
        // console.log(data)
        app.api_request('?c=user_order','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    listorder: res.listorder
                })
            }
        })
    },

    getinvoice(e) {
        var serialNos = e.currentTarget.dataset.invoice
        const data = { serialNos }
        tt.showLoading({
            title: '正在获取…',
        })
        app.api_request('nuonuo/queryInvoiceResult.php','POST',data).then(function (res) {
            if (res.code) {
                let url = res.data.url
                // console.log(url)
                tt.previewImage({
                    current: url,
                    urls: [url]
                })
            } else {
                tt.showToast({
                    icon: 'none',
                    title: "获取失败，请稍候再试",
                })
            }
            tt.hideLoading()
        })
    },

    getcontract(e) {
        var that = this
        var id = e.currentTarget.dataset.id
        const { uid,openid } = that.data.userinfo
        const data = {uid,openid,id}
        // console.log(data)
        tt.showLoading({
            title: '正在获取…',
        })
        app.api_request('?c=user_contract','GET',data).then(function (res) {
            if (res) {
                let contract = res.contract
                // console.log(url)
                tt.previewImage({
                    current: contract,
                    urls: [contract]
                })
                tt.hideLoading()
            }
        })
    },

    refund(e) {
        var that = this
        tt.showModal({
            title: '退款提示',
            content: '退款申请提交后，将在24小时内完成审核，审核通过后将原路退回，请再次确认是否退款？',
            success: function (res) {
                if (res.confirm) {
                    var id = e.currentTarget.dataset.id
                    const { uid,openid } = that.data.userinfo
                    const data = {uid,openid,id}
                    // console.log(data)
                    app.api_request('?c=user_order_refund','GET',data).then(function (res) {
                        tt.showModal({
                            //title: '提示',
                            content: res.tipmsg,
                            showCancel: false,
                            success: function () {
                                if (res.state == 1) {
                                    that.getlist()
                                }
                            }
                        })
                    })
                }
            }
        })
    },

    applyRefund(e) {
        if (tt.canIUse("industrySDK.pay-button")) {
            // do something
            console.log('支持')
        }
        // 判断是否支持交易模版2.0 和 pay-button 组件
        if (tt.canIUse("microapp-trade-plugin")) {
            // do something
            console.log('支持')
        }

        const {
            orderId
        } = e.detail;

        const extra = {
            orderId
        };
        return new Promise(resolve => {
            resolve(extra);
        });
    }
})